package com.goldgov.meetingstatistics.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.meetingstatistics.bean.MeetingOrg;
import com.goldgov.meetingstatistics.bean.MeetingOrgQuery;
import com.goldgov.meetingstatistics.bean.MeetingParamsBean;
import com.goldgov.meetingstatistics.bean.MeetingStatisticsBean;
import com.goldgov.meetingstatistics.bean.OrgMeetingNumber;
import com.goldgov.meetingstatistics.bean.OrgMeetingSign;
import com.goldgov.meetingstatistics.service.MeetingStatisticsService;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: MeetingStatisticsController 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年10月12日 下午4:15:28 
 */
@RestController
@RequestMapping("/workbench/meetingStatistics")
@Api("会议统计接口")
public class MeetingStatisticsController {
	
	@Autowired
	private MeetingStatisticsService meetingStatisticsService;
	
	
	
	@GetMapping("/getMeetingTime")
	@ApiOperation(value = "三会一课会议次数", notes ="三会一课会议次数")
	public JsonSuccessObject<MeetingStatisticsBean> getMeetingTime(MeetingParamsBean obj,HttpServletRequest request){
		if (!PropertyUtil.objectNotEmpty(obj.getSearchYear())) {
			obj.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<OrgMeetingNumber> list = meetingStatisticsService.getMeetingTime(obj);
		MeetingStatisticsBean bean = new MeetingStatisticsBean();
		bean.setMeetingNumberList(list);
		JsonSuccessObject<MeetingStatisticsBean> returnResult = new JsonSuccessObject<MeetingStatisticsBean>(bean);
		return returnResult;
	}
	@GetMapping("/getMeetingSign")
	@ApiOperation(value = "三会一课考勤统计", notes ="三会一课考勤统计")
	public JsonSuccessObject<MeetingStatisticsBean> getMeetingSign(MeetingParamsBean obj,HttpServletRequest request){
		if (!PropertyUtil.objectNotEmpty(obj.getSearchYear())) {
			obj.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<OrgMeetingSign> list = meetingStatisticsService.getMeetingSign(obj);
		MeetingStatisticsBean bean = new MeetingStatisticsBean();
		bean.setMeetingSignList(list);
		JsonSuccessObject<MeetingStatisticsBean> returnResult = new JsonSuccessObject<MeetingStatisticsBean>(bean);
		return returnResult;
	}
	
	
	/**
	 * 整体完成率统计
	 */
	@GetMapping("/countOrgFinishRateByYear")
	@ApiOperation(value = "整体完成率统计", notes = "整体完成率统计")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "obj"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "obj"),
			@ApiImplicitParam(name = "searchMeetingType", value = "会议类型", paramType = "obj"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "obj")})
	public JsonSuccessObject<MeetingStatisticsBean> countOrgFinishRateByYear(@ApiIgnore MeetingParamsBean obj,HttpServletRequest request)throws Exception{
		if (!PropertyUtil.objectNotEmpty(obj.getSearchBeginDate())) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			obj.setSearchBeginDate(sdf.format(new Date()));
		}
		//按年统计，取得当前年
		String year=obj.getSearchBeginDate().split("-")[0];
		obj.setSearchBeginDate(year+"-01");
		obj.setSearchEndDate(year+"-12");
		MeetingStatisticsBean bean=meetingStatisticsService.countOrgFinishRateByYear(obj);
		JsonSuccessObject<MeetingStatisticsBean> returnResult = new JsonSuccessObject<MeetingStatisticsBean>(bean);
		return returnResult;
	}
	
	@PostMapping("/findOrgOrMeetingNumList")
	@ApiOperation(value = "查询会议次数点击是弹出统计各支部会议次数或考勤", notes ="查询会议次数点击是弹出统计各支部会议次数或考勤")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchOrganizationID", value = "组织id", paramType = "query"),
		 @ApiImplicitParam(name = "searchOrganizationName", value = "组织名称", paramType = "query"),
		 @ApiImplicitParam(name = "searchYear", value = "年份", paramType = "query"),
		 @ApiImplicitParam(name = "searchQuarter", value = "季度", paramType = "query"),
		 @ApiImplicitParam(name = "searchMonth", value = "月份", paramType = "query"),
		 @ApiImplicitParam(name = "searchMeetingNumOrSignType", value = "查询类型", paramType = "query")})
	public JsonQueryObject<MeetingOrg> findOrgOrMeetingNumList(@ApiIgnore MeetingOrgQuery query,HttpServletRequest request){
		List<MeetingOrg> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(query.getSearchMeetingNumOrSignType()) && query.getSearchMeetingNumOrSignType().intValue() == 2) {
			//查询考勤
			list = meetingStatisticsService.findOrgAndMeetingSignList(query);
		}else {
			//查询次数
			list = meetingStatisticsService.findOrgOrMeetingNumList(query);
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	} 
	
	
	
	@GetMapping("/getDisMeetingTime")
	@ApiOperation(value = "远程教育会议次数", notes ="远程教育会议次数")
	public JsonSuccessObject<MeetingStatisticsBean> getDisMeetingTime(MeetingParamsBean obj,HttpServletRequest request){
		if (!PropertyUtil.objectNotEmpty(obj.getSearchYear())) {
			obj.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<OrgMeetingNumber> list = meetingStatisticsService.getDisMeetingTime(obj);
		MeetingStatisticsBean bean = new MeetingStatisticsBean();
		bean.setMeetingNumberList(list);
		JsonSuccessObject<MeetingStatisticsBean> returnResult = new JsonSuccessObject<MeetingStatisticsBean>(bean);
		return returnResult;
	}
	@GetMapping("/getDisMeetingSign")
	@ApiOperation(value = "远程教育考勤统计", notes ="远程教育考勤统计")
	public JsonSuccessObject<MeetingStatisticsBean> getDisMeetingSign(MeetingParamsBean obj,HttpServletRequest request){
		if (!PropertyUtil.objectNotEmpty(obj.getSearchYear())) {
			obj.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<OrgMeetingSign> list = meetingStatisticsService.getDisMeetingSign(obj);
		MeetingStatisticsBean bean = new MeetingStatisticsBean();
		bean.setMeetingSignList(list);
		JsonSuccessObject<MeetingStatisticsBean> returnResult = new JsonSuccessObject<MeetingStatisticsBean>(bean);
		return returnResult;
	}
	
	@PostMapping("/findOrgDisMeetingNumOrSignList")
	@ApiOperation(value = "查询远程教育会议次数点击是弹出统计各支部会议次数或考勤", notes ="查询会议次数点击是弹出统计各支部会议次数或考勤")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchOrganizationID", value = "组织id", paramType = "query"),
		 @ApiImplicitParam(name = "searchOrganizationName", value = "组织名称", paramType = "query"),
		 @ApiImplicitParam(name = "searchYear", value = "年份", paramType = "query"),
		 @ApiImplicitParam(name = "searchQuarter", value = "季度", paramType = "query"),
		 @ApiImplicitParam(name = "searchMonth", value = "月份", paramType = "query"),
		 @ApiImplicitParam(name = "searchMeetingNumOrSignType", value = "查询类型 1查询次数 2查询考勤", paramType = "query")})
	public JsonQueryObject<MeetingOrg> findOrgDisMeetingNumOrSignList(@ApiIgnore MeetingOrgQuery query,HttpServletRequest request){
		if (!PropertyUtil.objectNotEmpty(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<MeetingOrg> list = meetingStatisticsService.findOrgDisMeetingNumOrSignList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	} 
	
	
}
